package org.dromara.authcode.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.authcode.domain.bo.TDeviceOrderBo;
import org.dromara.authcode.domain.vo.TDeviceOrderVo;
import org.dromara.authcode.domain.TDeviceOrder;
import org.dromara.authcode.mapper.TDeviceOrderMapper;
import org.dromara.authcode.service.ITDeviceOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备订购信息Service业务层处理
 *
 * @author Miles Meng
 * @date 2025-08-12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TDeviceOrderServiceImpl implements ITDeviceOrderService {

    private final TDeviceOrderMapper baseMapper;

    /**
     * 查询设备订购信息
     *
     * @param id 主键
     * @return 设备订购信息
     */
    @Override
    public TDeviceOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询设备订购信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 设备订购信息分页列表
     */
    @Override
    public TableDataInfo<TDeviceOrderVo> queryPageList(TDeviceOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TDeviceOrder> lqw = buildQueryWrapper(bo);
        Page<TDeviceOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的设备订购信息列表
     *
     * @param bo 查询条件
     * @return 设备订购信息列表
     */
    @Override
    public List<TDeviceOrderVo> queryList(TDeviceOrderBo bo) {
        LambdaQueryWrapper<TDeviceOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 构建查询条件Wrapper ，查询最新的设备订购信息
     *
     * @param bo 设备ID，商品 ID
     * @return 设备订购信息
     */
    @Override
    public TDeviceOrderVo getLastDeviceOrder(TDeviceOrderBo bo) {
        LambdaQueryWrapper<TDeviceOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TDeviceOrder::getDeviceId, bo.getDeviceId());
        lqw.eq(TDeviceOrder::getGoodsId, bo.getGoodsId());
        lqw.orderByDesc(TDeviceOrder::getId);
        lqw.last("limit 1");
        return baseMapper.selectVoOne(lqw);
    }
    private LambdaQueryWrapper<TDeviceOrder> buildQueryWrapper(TDeviceOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TDeviceOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TDeviceOrder::getId);
        lqw.eq(bo.getDeviceId() != null, TDeviceOrder::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getGoodsId() != null, TDeviceOrder::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getExpireTime() != null, TDeviceOrder::getExpireTime, bo.getExpireTime());
        return lqw;
    }

    /**
     * 新增设备订购信息
     *
     * @param bo 设备订购信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TDeviceOrderBo bo) {
        TDeviceOrder add = MapstructUtils.convert(bo, TDeviceOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备订购信息
     *
     * @param bo 设备订购信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TDeviceOrderBo bo) {
        TDeviceOrder update = MapstructUtils.convert(bo, TDeviceOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TDeviceOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除设备订购信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
